package com.book.upms.ip.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.book.common.result.Failed;
import com.book.common.result.RestResponse;
import com.book.common.result.Success;
import com.book.common.result.PageInfo;
import com.book.common.api.helper.TransformHelper;
import com.book.common.api.entity.CurrentUser;
import com.book.upms.ip.entity.BlackIp;
import com.book.upms.ip.dto.BlackIpDto;
import com.book.upms.ip.vo.BlackIpVo;
import com.book.upms.ip.service.IBlackIpService;
import com.book.common.api.core.RESTfulMethod;
import com.book.modules.core.controller.BaseController;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import com.book.modules.annotation.InjectionCurrentUser;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * Description：
 * @Author： leo
 * @CreateDate： 2020-12-31
 * @Email： leo.xiong@suyun360.com
 * Version: 1.0
 */
@Slf4j
@Validated
@RestController
@Api(value = "IP黑名单信息")
@RequestMapping(value = "${adminPath}/blackIp")
public class BlackIpController extends BaseController{

    @Autowired
    private IBlackIpService blackIpService;

    /**
     * 根据ID获取IP黑名单信息
     *
     * @param id
     * @param currentUser
     * @return
     */
    @GetMapping(value = "{id}", consumes = "application/json")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "根据id获取IP黑名单信息", httpMethod = RESTfulMethod.GET, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "IP黑名单ID", required = true)
    })
    public RestResponse get(@NotBlank(message = "{required}") @PathVariable String id, @ApiIgnore CurrentUser currentUser) {
        try {
            if (StringUtils.isEmpty(id)) {
                return new Failed("BOOK:COMMON:10003", i18nHelper.get("BOOK:COMMON:10003"));
            }
            return new Success(this.blackIpService.getById(id), i18nHelper.get("BOOK:COMMON:10004"));
        } catch (Exception e) {
            log.error("查询IP黑名单失败",e);
            return new Failed("BOOK:COMMON:10005", i18nHelper.get("BOOK:COMMON:10005")+e.getMessage());
        }
    }

    /**
     * 查询IP黑名单信息
     *
     * @param blackIpDto
     * @param currentUser
     * @return
     */
    @PostMapping(value = "list")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "查询IP黑名单信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse list(@RequestBody BlackIpDto blackIpDto, @ApiIgnore CurrentUser currentUser) {
        try {
            BlackIp blackIp = TransformHelper.transform(blackIpDto,BlackIp.class);
            List<BlackIp> blackIpList=this.blackIpService.findAllList(blackIp);
            return new Success(TransformHelper.transformList(blackIpList,BlackIpVo.class), i18nHelper.get("BOOK:COMMON:10004"));
        } catch (Exception e) {
            log.error("查询IP黑名单失败", e);
            return new Failed("BOOK:COMMON:10006", i18nHelper.get("BOOK:COMMON:10006")+e.getMessage());
        }
    }

    /**
     * 查询IP黑名单信息（分页）
     *
     * @param blackIpDto
     * @param currentUser
     * @return
     */
    @PostMapping(value = "page/list")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "查询IP黑名单分页信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse pageList(@RequestBody BlackIpDto blackIpDto, @ApiIgnore CurrentUser currentUser) {
        try {
            BlackIp blackIp = TransformHelper.transform(blackIpDto,BlackIp.class);
            PageInfo<BlackIp> page = new PageInfo(blackIpDto.getPageNo(),blackIpDto.getPageSize());
            page = this.blackIpService.findPageList(blackIp,page);
            return new Success(TransformHelper.transformPage(page,BlackIpVo.class), i18nHelper.get("BOOK:COMMON:10004"));
        } catch (Exception e) {
            log.error("分页查询IP黑名单失败", e);
            return new Failed("BOOK:COMMON:10007", i18nHelper.get("BOOK:COMMON:10007")+e.getMessage());
        }
    }

    /**
     * 新增IP黑名单信息
     *
     * @param blackIpDto
     * @param currentUser
     * @return
     */
    @PostMapping(value = "save")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "保存IP黑名单信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse save(@Valid @RequestBody BlackIpDto blackIpDto, @ApiIgnore CurrentUser currentUser) {
        try {
            this.blackIpService.save(TransformHelper.transform(blackIpDto,BlackIp.class));
            return new Success("", i18nHelper.get("BOOK:COMMON:10008"));
        } catch (Exception e) {
            log.error("新增IP黑名单失败", e);
            return new Failed("BOOK:COMMON:10009", i18nHelper.get("BOOK:COMMON:10009")+e.getMessage());
        }
    }

    /**
     * 修改IP黑名单信息
     *
     * @param blackIpDto
     * @param currentUser
     * @return
     */
    @PutMapping(value = "update")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "修改IP黑名单信息", httpMethod = RESTfulMethod.PUT, response = Success.class)
    public RestResponse update(@Valid @RequestBody BlackIpDto blackIpDto, @ApiIgnore CurrentUser currentUser) {
        try {
            if (blackIpDto==null || blackIpDto.getId() == null) {
                return new Failed("BOOK:COMMON:10010", i18nHelper.get("BOOK:COMMON:10010"));
            }
            this.blackIpService.update(Wrappers.update(TransformHelper.transform(blackIpDto,BlackIp.class)));
            return new Success(i18nHelper.get("BOOK:COMMON:10011"));
        } catch (Exception e) {
            log.error("修改IP黑名单失败", e);
            return new Failed("BOOK:COMMON:10012", i18nHelper.get("BOOK:COMMON:10012")+e.getMessage());
        }
    }

    /**
     * 删除IP黑名单信息
     *
     * @param id
     * @param currentUser
     * @return
     */
    @DeleteMapping(value = "delete/{id}")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "根据ID删除IP黑名单信息", httpMethod = RESTfulMethod.DELETE, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "IP黑名单ID", required = true)
    })
    public RestResponse delete(@NotBlank(message = "{required}") @PathVariable String id, @ApiIgnore CurrentUser currentUser) {
        try {
            this.blackIpService.removeByIds(Lists.newArrayList(id.split(StringPool.COMMA)));
            return new Success("", i18nHelper.get("BOOK:COMMON:10013"));
        } catch (Exception e) {
            log.error("删除IP黑名单失败", e);
            return new Failed("BOOK:COMMON:10014", i18nHelper.get("BOOK:COMMON:10014")+e.getMessage());
        }
    }

    /**
     * 导出IP黑名单模板信息
     *
     * @param currentUser
     * @return
     */
    @PostMapping(value = "export/template")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "导出IP黑名单模板信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse exportTemplate(@ApiIgnore CurrentUser currentUser) {
        try {
            super.excelHelper.exportExcel("IP黑名单",BlackIpVo.class);
            return new Success("", i18nHelper.get("BOOK:COMMON:10015"));
        } catch (Exception e) {
            log.error("导出IP黑名单模板信息失败", e);
            return new Failed("BOOK:COMMON:10016", i18nHelper.get("BOOK:COMMON:10016")+e.getMessage());
        }
    }

    /**
     * 导出IP黑名单信息
     *
     * @param blackIpDto
     * @param currentUser
     * @return
     */
    @PostMapping(value = "export/excel")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "导出IP黑名单信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse exportExcel(@RequestBody BlackIpDto blackIpDto, @ApiIgnore CurrentUser currentUser){
        try {
            List<BlackIp> blackIpList = this.blackIpService.findAllList(TransformHelper.transform(blackIpDto,BlackIp.class));
            super.excelHelper.exportExcel("IP黑名单",BlackIpVo.class,TransformHelper.transformList(blackIpList,BlackIpVo.class));
            return new Success("", i18nHelper.get("BOOK:COMMON:10024"));
        } catch (Exception e) {
            log.error("导出IP黑名单信息失败", e);
            return new Failed("BOOK:COMMON:10025", i18nHelper.get("BOOK:COMMON:10025")+e.getMessage());
        }
    }

    /**
     * 导入IP黑名单信息
     *
     * @param filePath
     * @param currentUser
     * @return
     */
    @PostMapping("import/excel")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "导入IP黑名单信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "filePath", value = "保存文件路径", required = true)
    })
    public RestResponse importExcel(@RequestParam(value = "filePath") String filePath, @ApiIgnore CurrentUser currentUser){
        try {
            List<BlackIpVo> blackIpVoList = super.excelHelper.importExcel(filePath,BlackIpVo.class);
            if(CollectionUtils.isEmpty(blackIpVoList)){
                return new Failed("BOOK:COMMON:10026", i18nHelper.get("BOOK:COMMON:10026"));
            }
            this.blackIpService.saveBatch(TransformHelper.transformList(blackIpVoList,BlackIp.class));
            return new Success("", i18nHelper.get("BOOK:COMMON:10027"));
        } catch (Exception e) {
            log.error("导入IP黑名单信息失败", e);
            return new Failed("BOOK:COMMON:10028", i18nHelper.get("BOOK:COMMON:10028")+e.getMessage());
        }
    }
}
